﻿using System.IO;
using System.Resources;
using System.Web;
using Wedn.Net.Common;

namespace Wedn.Net.Utility
{
    /// <summary>
    /// HtmlHelper拓展类(Culture)
    /// </summary>
    public static class CultureHelper
    {
        #region 获取语言化字符串 +static string GetCultureString(string key)
        /// <summary>
        /// 获取语言化字符串
        /// </summary>
        /// <param name="key">原文键</param>
        /// <returns>语言化字符串</returns>
        public static string GetCultureString(string key)
        {
            if (HttpContext.Current == null) return key;
            var vPath = string.Format("~/Content/Culture/lang.{0}.resources", Setting.Culture);
            var fPath = HttpContext.Current.Server.MapPath(vPath);
            if (!File.Exists(fPath)) fPath = HttpContext.Current.Server.MapPath("~/Content/Culture/lang.resources");
            if (!File.Exists(fPath)) return key;
            using (ResourceSet set = new ResourceSet(fPath))
            {
                //CacheHelper.SetCache(vPath, set);
                return set.GetString(key);
            }
        }
        #endregion

        #region 获取语言化字符串 +static string GetCultureString(this HttpContext context, string key)
        /// <summary>
        /// 获取语言化字符串
        /// </summary>
        /// <param name="context">HttpContext</param>
        /// <param name="key">原文键</param>
        /// <returns>语言化字符串</returns>
        public static string GetCultureString(this HttpContext context, string key)
        {
            if (context == null) return key;
            var vPath = string.Format("~/Content/Culture/lang.{0}.resources", Setting.Culture);
            var fPath = context.Server.MapPath(vPath);
            if (!File.Exists(fPath)) fPath = context.Server.MapPath("~/Content/Culture/lang.resources");
            if (!File.Exists(fPath)) return key;
            using (ResourceSet set = new ResourceSet(fPath))
            {
                //CacheHelper.SetCache(vPath, set);
                return set.GetString(key);
            }
        }
        #endregion
    }
}
